package main

import "sort"

func main() {
	
}
//#link: https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/
func maxFrequency(nums []int, k int) int {
	sort.Ints(nums)
	ans := 1
	for l, r, total := 0, 1, 0; r < len(nums); r++ {
		//计算区间内每个值，与区间内最后一个值相差的总和
		total += (nums[r] - nums[r-1]) * (r - l)
		//若差值大于k的话，左区间整体右移一位
		for total > k {
			total -= nums[r] - nums[l]
			l++
		}
		ans = max(ans, r-l+1)
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
